//@ sourceURL=/static/modules/datasource_manage/datasource_manage.js

let searchUrl = '/dataSource/find';
let addUrl = '/dataSource/add';
let editUrl = '/dataSource/edit';
let testUrl = '/dataSource/test';
let deleteUrl = '/dataSource/delete';
let viewType;
var datasource_manageView = new View({
    name: 'datasource_manage',
    init_page() {
        console.log('datasource_manage-load');
        $('.container-fluid [data-plugin="selectpicker"]').selectpicker($.po('selectpicker', {
            container: 'body'
        }))
        $('#datasourcemanage_add_container [data-plugin="selectpicker"]').selectpicker($.po('selectpicker', {
            container: 'body',
            width: '340px'
        }))
    }
});
/**
 * 方法
 */
datasource_manageView.fn = {
    testConnection() {
        delete(datasource_manageView.plugins.datasourcemanage_add_form.options.add_url)
        delete(datasource_manageView.plugins.datasourcemanage_add_form.options.add_url)
        if (datasource_manageView.plugins.datasourcemanage_add_form.options.type == 'edit') {
            datasource_manageView.plugins.datasourcemanage_add_form.options.edit_url = testUrl;
        }

        datasource_manageView.plugins.datasourcemanage_add_form.options.url = testUrl;
        datasource_manageView.plugins.datasourcemanage_add_form.$form.submit()

    },
    dataSourceEdit(type) {
        delete(datasource_manageView.plugins.datasourcemanage_add_form.options.add_url)
        delete(datasource_manageView.plugins.datasourcemanage_add_form.options.add_url)
        if (datasource_manageView.plugins.datasourcemanage_add_form.options.type == 'edit') {
            datasource_manageView.plugins.datasourcemanage_add_form.options.url = editUrl;
            datasource_manageView.plugins.datasourcemanage_add_form.options.edit_url = editUrl;
        } else {
            datasource_manageView.plugins.datasourcemanage_add_form.options.url = addUrl;
        }
        datasource_manageView.plugins.datasourcemanage_add_form.$form.submit()
    },
    edit(e, num) {
        $('.connStatus').val(0)
        viewType = num;
        if (viewType == 0) {
            datasource_manageView.plugins.datasourcemanage_add_form.options.type = "add"
            datasource_manageView.plugins.datasourcemanage_add_dialog.options.title = '添加数据源';
        } else {
            datasource_manageView.plugins.datasourcemanage_add_form.options.type = "edit"
            datasource_manageView.plugins.datasourcemanage_add_dialog.options.title = '编辑数据源';
            datasource_manageView.plugins.datasourcemanage_add_form.options.params = {
                id: $(e).data().id
            }
        }
        datasource_manageView.plugins.datasourcemanage_add_dialog.show();
    },
    delete(e) {
        $('.connStatus').val(0);
        let id = $(e).data('id');
        swal($.po('sweetalert', {
            title: "确定删除吗？",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: '#DD6B55',
            confirmButtonText: '删除',
            cancelButtonText: "取消",
            closeOnConfirm: false,
        }), function (isConfirm) {
            if (isConfirm) {
                wdAjax.ajax({
                    type: 'get',
                    url: deleteUrl + '?id=' + id,
                    success: function (data) {
                        alertify.success('删除成功');
                        swal.close();
                        datasource_manageView.plugins.datasource_manage_list.api.draw();
                    }
                })
            }
        });
    },
    chooseData(e) {
        $('#datasourcemanage_add_container input[name="dbName"]').val('')
        datasource_manageView.plugins.datasourcemanage_add_form.formValidation.resetField($('#datasourcemanage_add_container [name=dbName]'), '')
        let dbNameValidators = datasource_manageView.plugins.datasourcemanage_add_form.formValidation.options.fields.dbName.validators;
        let dbNameStringLengthValidator = dbNameValidators.stringLength;
        if ($('.dataType option:selected').val() === "0") {
            $('#datasourcemanage_add_container .form-db-name').removeClass('hide');
            $('#datasourcemanage_add_container .add-sid').removeClass('hide');
            $('#datasourcemanage_add_container .add-dbName').addClass('hide');
            dbNameStringLengthValidator.max = 8;
            dbNameStringLengthValidator.message = 'SID不能为空, 最多可输入8个字符';
        } else if ($('.dataType option:selected').val() === "1") {
            $('#datasourcemanage_add_container .form-db-name').removeClass('hide');
            $('#datasourcemanage_add_container .add-sid').addClass('hide');
            $('#datasourcemanage_add_container .add-dbName').removeClass('hide');
            dbNameStringLengthValidator.max = 20;
            dbNameStringLengthValidator.message = 'SID不能为空,最多可输入20个字符';
        } else {
            $('#datasourcemanage_add_container .form-db-name').addClass('hide')
        }
    },
    render: {
        operation(data, type) {
            var html = '<button class="btn btn-primary margin-right-10 grid-edit-btn" data-name="' + data.userName + '" data-id="' + data.id + '" type="button" onClick="datasource_manageView.fn.edit(this,1)">编辑</button>' + '<button class="btn btn-primary margin-right-10 grid-delete-btn" data-name="' + data.userName + '" data-id="' + data.id + '" type="button" onClick="datasource_manageView.fn.delete(this)" >删除</button>'
            return html;
        }
    }
}
/**
 * 事件
 */
datasource_manageView._event = () => {

}
/**
 * 绑定插件
 */
datasource_manageView.use(new Table({
    name: 'datasource_manage_list',
    selector: '#datasource_manage_list',
    url: searchUrl,
    search_btn: "#search_btn",
    callback: function () {

    }
}))
datasource_manageView.use(new Dialog({
    name: 'datasourcemanage_add_dialog',
    container_selector: '#datasourcemanage_add_container',
    init_callback: function () {
        datasource_manageView.plugins['datasourcemanage_add_form'].init();
    }
}))
datasource_manageView.use(new Form({
    name: 'datasourcemanage_add_form',
    selector: '#datasourcemanage_add_form',
    edit_url: editUrl,
    add_url: addUrl,
    delay: true,
    init_callback() {
        let self = this;
        if (viewType == 0) {
            $('.selectpickerAdd').selectpicker('val', '');
            $('.selectpickerAdd').removeAttr('disabled');
            $('.form-group-dataType .btn-group>.btn:first-child').removeClass('disabled');
            datasource_manageView.fn.chooseData();
        } else if (viewType == 1) {
            wdAjax.post({
                url: searchUrl,
                data: this.options.params || {},
                success: function (data) {
                    $('.selectpickerAdd').selectpicker('val', data.list[0].dataType);
                    $('.selectpickerAdd').attr('disabled','disabled');
                    $('.form-group-dataType .btn-group > .btn:first-child').addClass('disabled');
                    datasource_manageView.fn.chooseData();
                    $('.dsName').val(data.list[0].dsName);
                    $('.ip').val(data.list[0].ip);
                    $('.port').val(data.list[0].port);
                    $('.username').val(data.list[0].userName);
                    $('.password').val(data.list[0].password);
                    $('.sid').val(data.list[0].dbName);
                    // $('.dbName').val(data.list[0].dbName);
                    $('.id').val(this.data.id);
                }
            })
        }
    },
    before_callback(data) {
        if (!data['dataType']) {
            data['dataType'] = $('.selectpickerAdd').val();
        }
        return data;
    },
    success: function (data) {
        if (datasource_manageView.plugins.datasourcemanage_add_form.options.url == testUrl) {
            alertify.success('连接成功!');
            $('.connStatus').val(1)
        } else {
            if (viewType == 0) {
                alertify.success('添加成功!');
            } else {
                alertify.success('修改成功!');
            }
            datasource_manageView.plugins.datasourcemanage_add_dialog.close();
        }
        datasource_manageView.plugins.datasource_manage_list.api.draw();
    },
    error_callback: function (data) {
        if (datasource_manageView.plugins.datasourcemanage_add_form.options.url == testUrl) {
            if (data.code == 47) {
                $('.connStatus').val(2);
            }
        }
    }
}))